package cn.tedu._04weibo.controller;

import cn.tedu._04weibo.base.response.JsonResult;
import cn.tedu._04weibo.base.response.StatusCode;
import cn.tedu._04weibo.mapper.WeiboMapper;
import cn.tedu._04weibo.pojo.dto.WeiboAddParam;
import cn.tedu._04weibo.pojo.entiy.Weibo;
import cn.tedu._04weibo.pojo.vo.UserVO;
import cn.tedu._04weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._04weibo.pojo.vo.WeiboIndexVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.stylesheets.LinkStyle;

import java.util.Date;
import java.util.List;
@Validated
@Tag(name = "02-微博模块")
@RestController
@Slf4j
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @PostMapping("insert")
    @Operation(summary = "发布微博")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(@RequestBody WeiboAddParam weiboAddParam, HttpSession session){
        log.debug("weiboAddParam = "+weiboAddParam);
        UserVO userVO= (UserVO)session.getAttribute("user");
//        if (userVO==null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//
//        }
        Weibo weibo=new Weibo();
        BeanUtils.copyProperties(weiboAddParam,weibo);
        weibo.setUserId(userVO.getId());
        weibo.setCreateTime(new Date());
        weiboMapper.insertWeibo(weibo);


        return JsonResult.ok();
    }
    @GetMapping("selectIndex")
    @Operation(summary = "微博首页列表")
    @ApiOperationSupport(order = 20)
    public JsonResult selectIndex( ){
        List<WeiboIndexVO> weiboIndexVOS=
                weiboMapper.selectIndex();


        return JsonResult.ok(weiboIndexVOS);
    }
    @GetMapping("selectById")
    @Operation(summary = "微博详情")
    @ApiOperationSupport(order = 30)
    public JsonResult selectById(@Range(min = 200,max = 500,message = "微博编号必须在200-500之间") Long id){
        log.debug("id = "+id);
        WeiboDetailVO weiboDetailVO=
                weiboMapper.selectById(id);


        return JsonResult.ok(weiboDetailVO);
    }
}
